package com.pda.gongpin.ui.fulltrayin;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.pda.gongpin.R;
import com.pda.gongpin.adapter.FullTrayInListAdapter;
import com.pda.gongpin.bean.FullTrayInListBean;
import com.pda.gongpin.bean.request.SearchRequest;
import com.pda.gongpin.contract.FullTrayInListContract;
import com.pda.gongpin.presenter.FullTrayInListPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.FULLTRAYINLISTACTIVITY)
public class FullTrayInListActivity extends BaseMvpActivity<FullTrayInListPresenter> implements FullTrayInListContract.View, OnRefreshListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    FullTrayInListAdapter fullTrayInListAdapter;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.smart)
    SmartRefreshLayout smart;

    @Override
    public void setList(List<FullTrayInListBean> list) {
        smart.finishRefresh();
        fullTrayInListAdapter.setNewInstance(list);
    }

    @Override
    public FullTrayInListPresenter getPresenter() {
        return new FullTrayInListPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_list;
    }

    @Override
    public void initView() {
        smart.setEnableLoadMore(false);
        smart.setOnRefreshListener(this);
        tvTitle.setText("整托入库");
        fullTrayInListAdapter = new FullTrayInListAdapter(R.layout.item_full_list, new ArrayList<>());
        rvContent.setAdapter(fullTrayInListAdapter);

    }

    @Override
    public void initLinsenterner() {
        fullTrayInListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("entryNo", fullTrayInListAdapter.getData().get(position).getEntryNo());
                bundle.putString("erpCode", fullTrayInListAdapter.getData().get(position).getErpCode());
                startIntent(AppActivityKey.FULLTRAYINACTIVITY, bundle);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        smart.autoRefresh();
    }

    @Override
    public void getData() {

    }

    @OnClick({R.id.iv_back, R.id.tv_back, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finishActivity();
                break;
            case R.id.tv_back:
                break;
            case R.id.tv_search:
                presenter.getEntry(new SearchRequest(etSearch.getText().toString()));
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        presenter.getEntry(new SearchRequest(etSearch.getText().toString()));
    }

    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        smart.finishRefresh();
    }
}
